]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSMacOSX/Tests/BATS Scripts/bats_test_proxy.sh
mDNSResponder-1096.0.2.tar.gz
[apple/mdnsresponder.git] / mDNSMacOSX / Tests / BATS Scripts / bats_test_proxy.sh
1 #!/bin/sh
2 #
3 # bats_test_proxy.sh
4 # mDNSResponder Tests
5 #
6 # Copyright (c) 2019 Apple Inc. All rights reserved.
7
8 # tests whether the state dump will create at most MAX_NUM_DUMP_FILES, to avoid wasting too much space.
9 base_test="dnssdutil dnsquery -s 127.0.0.1 -n apple.com -t a"
10
11 # Enable the proxy and test over TCP
12 function test_proxy_tcp {
13 test_proxy_on $base_test --tcp
14 return $?
15 }
16
17 # Enable the proxy and test over UDP
18 function test_proxy_udp {
19 test_proxy_on $base_test
20 return $?
21 }
22
23 # Test the proxy over TCP without enabling it (should fail)
24 function test_noproxy_tcp {
25 test_proxy $base_test --tcp
26 if [ $? == 0 ]; then
27 return 1;
28 fi
29 return 0
30 }
31
32 # Test the proxy over UDP without enabling it (should fail)
33 function test_noproxy_udp {
34 test_proxy $base_test
35 if [ $? == 0 ]; then
36 return 1;
37 fi
38 return 0
39 }
40
41 function test_proxy_on {
42 local command_line="$*"
43 local ret=1
44
45 # Enable the proxy
46 dnssdutil dnsproxy -i lo0 &
47 local dnssdutil_pid=$!
48
49 # See if that worked
50 sleep 1
51 local dnssdutil_pid_now=$(ps xa |sed -n -e 's/^ *\([0-9][0-9]*\).*$/\1/' -e "/$dnssdutil_pid/p")
52 if [ $dnssdutil_pid != "$dnssdutil_pid_now" ]; then
53 echo "Failed to enable DNS proxy $dnssdutil_pid $dnssdutil_pid_now."
54 return 1
55 fi
56
57 test_proxy $command_line
58 ret=$?
59
60 # Disable the proxy and wait for that to finish
61 kill -HUP $dnssdutil_pid
62 wait $dnssdutil_pid
63 return $ret
64 }
65
66 function test_proxy {
67 local command_line="$*"
68 local ret=1
69 # Try to do the DNS lookup
70 local output=$($command_line |egrep "^End reason:")
71 if [ "$output" = "End reason: received response" ]; then
72 echo "Proxy is working: $output"
73 ret=0
74 else
75 echo "Proxy is not working: $output"
76 fi
77 return $ret
78 }
79
80 ret=0
81 # Functions are put inside an array, use ($test) to evaluate it
82 declare -a tests=("test_proxy_tcp"
83 "test_proxy_udp"
84 "test_noproxy_tcp"
85 "test_noproxy_udp")
86 echo ""
87 echo "----Proxy Test Start, $(date)----"
88 for test in "${tests[@]}"; do
89 echo "running $test:"
90 ($test)
91 if [[ $? -eq 0 ]]; then
92 echo "passed"$'\n' # use $'\n' to print one more newline character
93 else
94 ret=1
95 echo "failed"$'\n'
96 fi
97 done
98 echo "----Proxy Test End, $(date)----"
99 exit $ret
100
101 # Local Variables:
102 # tab-width: 4
103 # fill-column: 108
104 # indent-tabs-mode: nil
105 # End: